package com.mrd.gtimp.sup.action;

import com.mrd.constant.MsgConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.sup.entity.SupComConf;
import com.mrd.gtimp.sup.service.SupComConfBiz;
import com.mrd.util.LoginUtil;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author luob
 */

@Component
@RequestMapping("/sup")
public class SupComConfAction {
    private static final long serialVersionUID = 1L;
    @Resource
    private SupComConfBiz supComConfBiz;

    /**
     * 默认访问入口
     */
    @RequestMapping("/upComConf-main")
    public String main() {
        return "/gtimp/sup/supComConfManage";
    }

    /**
     * 新增或修改信息
     */
    //// TODO: 2016/8/15  @Permissions
//    @Permissions(permissionsId = RoleConstants.SUP_COMCON_SAVE)
    @RequestMapping("/supComConf-saveOrUpdate")
    @ResponseBody
    public Result saveOrUpdate(SupComConf supComConf) {
        Result msg;
        // if(checkRight(RoleConstants.SUP_COMCON_SAVE)) {
        try {
            if (StringUtils.isEmpty(supComConf.getID())) {  // 新增操作
                supComConf.setCOM_ID(LoginUtil.getLoginUser().getCompany().getId());
                supComConf.setCREATOR(LoginUtil.getLoginUser().getAccount());
                supComConf.setCTIME(new Date());
                supComConf.setID(null);
            } else {  // 修改操作
                supComConf.setMODIFIER(LoginUtil.getLoginUser().getAccount());
                supComConf.setMTIME(new Date());
            }
            supComConfBiz.saveOrupdateEntity(supComConf);
            msg = Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg = Result.newFailInstance(MsgConstants.SAVE_FAIL + e.getMessage());
        }
        return msg;
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/supComConf-find")
    @ResponseBody
    public SupComConf find(SupComConf supComConf) {
        if (null != supComConf) {
            if (StringUtils.isNotEmpty(supComConf.getID())) {
                try {
                    supComConf = supComConfBiz.findEntityById(supComConf.getID());
                    return supComConf;
                } catch (QueryException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 编辑信息
     */
    @RequestMapping("/supComConf-findByComId")
    @ResponseBody
    public Page<SupComConf> findByComId(Jqgrid jqgrid) {
        Object supComConf = null;
        String comId = LoginUtil.getLoginUser().getCompany().getId();
        try {
//            if (null == filters) {
//                filters = new Filter();
//                filters.setGroupOp(FilterGroupOption.AND);
//            }
            SearchRule rule = new SearchRule("COM_ID", SearchRuleOption.eq, comId);
            jqgrid.addRule(rule);
//            page = initPage(supComConfBiz.findEntityCount(filters));
//            List<SupComConf> result = supComConfBiz
//                    .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, null);
            Page<SupComConf> result = supComConfBiz.findByJqgrid(jqgrid);
            if (result.getSize() == 1) {
                supComConf = result.getContent().get(0);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    /**
     * 删除信息
     */
    @RequestMapping("/supComConf-del")
    public String del() {
        return "";
    }

    /**
     * 查询
     */
    @RequestMapping("/supComConf-findList")
    public String findList() {
        return null;
    }

    @Resource
    public void setSupComConfBiz(SupComConfBiz supComConfBiz) {
        this.supComConfBiz = supComConfBiz;
    }

}
